from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from work.models import Label
from utils.modelsMixin import ModelSetMixin


# 文章部分
class Article(ModelSetMixin):
    STATUS_CHOICE = (
        (0, '未发布'),
        (1, '发布'),
    )

    title = models.CharField('文章标题', max_length=100)
    digest = models.CharField('文章摘要', max_length=200)
    content = models.TextField('文章内容')
    page_view = models.IntegerField('浏览量', default=0)  # 设置默认值0
    priority = models.IntegerField('优先级', default=0)
    status = models.IntegerField('状态', choices=STATUS_CHOICE, default=0)

    label = models.ForeignKey(Label, on_delete=models.CASCADE)  # 级联(同步)删除
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        ordering = ['-priority', '-page_view', '-c_time']  # 根据优先级、浏览器、创建时间进行排序
        db_table = 'article'  # 创建对应的数据库
        verbose_name = '文章'
        verbose_name_plural = verbose_name


# 评论部分
class Comment(models.Model):
    content = models.TextField('评论内容')
    level = models.IntegerField('评论等级', default=1)  # 1评论文章 2回复评论
    parent_comment = models.IntegerField('评论者id', null=True, blank=True)  # 评论文章者的id,null=True允许没人评论,blank=True允许前端传空值
    reply_comment = models.IntegerField('回复者id', null=True, blank=True)  # 回复评论者id
    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    # 表关联
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        ordering = ['-create_time']
        db_table = 'comment'
        verbose_name = '评论'
        verbose_name_plural = verbose_name


